package cn.airfei.netty.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description:
 * @author: air
 * @create: 2020-12-30 14:58
 */
public class NIOFileChannel {
    public static void main(String[] args) throws IOException {
        String str = "hello air;您好 air";
        // 创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("/home/air/Documents/test.text");

        FileChannel fileChannel = fileOutputStream.getChannel();

        // 创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());

        // 将bytebuffer的写操作改为读
        byteBuffer.flip();
        // 将缓存区的文件写入fileChannel
        int write = fileChannel.write(byteBuffer);

        fileOutputStream.close();
    }
}
